//
//  VSFindClothesViewController.m
//  UI项目2.0
//
//  Created by dlios on 15/7/15.
//  Copyright (c) 2015年 星际科技. All rights reserved.
//

#import "VSFindClothesViewController.h"
#import "VSFindClothesWaterLayout.h"
#import "VSFindClothesCollectionViewCell.h"
#import "VSAFNetworkHandler.h"
#import "VSFindClothesModel.h"
#import "VSFrameGrand.h"
#import "VSFindClothesPushViewController.h"




@interface VSFindClothesViewController () <VSFindWaterLayoutDelegate,UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *ItmeArr;
@property (nonatomic, retain) VSFindClothesModel *ClothesModel;
@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation VSFindClothesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
}

- (void)collectionShow
{
    VSFindClothesWaterLayout *Layout = [[[VSFindClothesWaterLayout alloc]init]autorelease];
    self.collectionView = [[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:Layout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[VSFindClothesCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    UIImageView *backGroundImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backGroundImage.image = [UIImage imageNamed:@"background001.png"];
    self.collectionView.backgroundView = backGroundImage;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.ItmeArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VSFindClothesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    
    cell.model = self.ItmeArr[indexPath.item];
    return cell;
}

- (CGFloat)getLayoutHeight
{
    CGFloat itmeZ = 0.040 * SCREEN_WIDTH;
    CGFloat itmeY = 0.296 * SCREEN_WIDTH;
    CGFloat itmeX = 0.016 * SCREEN_WIDTH;
    if (self.ItmeArr.count % 12 == 1 || self.ItmeArr.count % 12 == 2 || self.ItmeArr.count % 12 == 3) {
        return itmeY * 2 + itmeX + itmeZ + 50 + (self.ItmeArr.count / 13) * 6 * (itmeY + itmeX);
    } else if (self.ItmeArr.count % 12 == 4 || self.ItmeArr.count % 12 == 5 || self.ItmeArr.count % 12 == 6) {
        return itmeY * 3 + itmeX * 2 + itmeZ + 50 + (self.ItmeArr.count / 13) * 6 * (itmeY + itmeX);
    } else if (self.ItmeArr.count % 12 == 7 || self.ItmeArr.count % 12 == 8 || self.ItmeArr.count % 12 == 9) {
       return  itmeY * 5 + itmeX * 4 + itmeZ + 50 + (self.ItmeArr.count / 13) * 6 * (itmeY + itmeX);
    } else if (self.ItmeArr.count % 12 == 10 || self.ItmeArr.count % 12 == 11 || self.ItmeArr.count % 12 == 12) {
        return itmeZ + itmeY * 6 + itmeX * 5 + 50 + (self.ItmeArr.count / 13) * 6 * (itmeY + itmeX);
    }
   
    return 0;
}

- (void)getData
{
    NSString *tempStr = @"http://api.liwushuo.com/v1/item_subcategories/99/items?limit=40&offset=0";
    [VSAFNetworkHandler GETwithAFNbyURL:tempStr completion:^(id result) {
        NSArray *tempArr = [[result objectForKey:@"data"]objectForKey:@"items"];
        self.ItmeArr = [NSMutableArray array];
        for (NSDictionary *dic in tempArr) {
            self.ClothesModel = [VSFindClothesModel baseModelWithDic:dic];
            [self.ItmeArr addObject:self.ClothesModel];
        }
        [self collectionShow];
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    VSFindClothesPushViewController *new = [[[VSFindClothesPushViewController alloc]init]autorelease];
    VSFindClothesModel *model = self.ItmeArr[indexPath.item];
    new.url = model.url;
    [self.navigationController pushViewController:new animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
